package com.cola.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.cola.common.CookieConstant;
import com.cola.entity.WeChatInfo;
import com.cola.utils.CookieUtils;
import com.cola.utils.jedis.JedisCacheDB;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import static com.opensymphony.xwork2.Action.LOGIN;

/**
 * 微信拦截器
 * @author Max
 */
public class WeChatInterceptor extends AbstractInterceptor  {
	private static final Logger logger = Logger.getLogger(WeChatInterceptor.class);
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		String redisKey = CookieUtils.getCookieValue(ServletActionContext.getRequest()
				, CookieConstant.COLA_ORDERING_SYSTEM_WX_INFO_KEY);

		if(StringUtils.isNotEmpty(redisKey)){

			Object redisInfo = JedisCacheDB.getObject(redisKey);

			if(redisInfo!=null){
				WeChatInfo weChatInfo = JSONObject.parseObject(redisInfo.toString(),WeChatInfo.class);

				if(weChatInfo.getOpenid()!=null && weChatInfo.getNickname()!=null){
					return invocation.invoke();
				}
			}
		}
 		ServletActionContext.getRequest().setAttribute("error_msg","登陆已过期，请重新登录！");
		return LOGIN;
	}

}
